﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace TestForm
{
    public partial class Form1 : Form
    {
        public int Zoom;

        public Bitmap Canvas;

        public Graphics Graphics;

        public Snake Snake;

        public Timer Timer;

        public int Speed;

        public Assist Assist;

        public Form1()
        {
            InitializeComponent();

            Snake = new Snake(15, 15);
            Snake.GameOver += GameOver;

            Assist = new Assist();

            Zoom = 10;

            Speed = 1;

            Canvas = new Bitmap(Snake.Width * Zoom, Snake.Height * Zoom);

            Graphics = Graphics.FromImage(Canvas);

            Timer = new Timer();

            Timer.Interval = Speed;
            Timer.Tick += Timer_Tick;

            this.ClientSize = Canvas.Size;

            this.Show();
        }

        private void GameOver()
        {
            Timer.Stop();

            BtnPlay.Visible = true;

            MessageBox.Show("游戏结束，得分 : " + Snake.Score);
        }

        private void PrintFrame(Graphics G)
        {
            bool IsFirst = true;

            Graphics.Clear(Color.Black);

            foreach (var Item in Snake.Bodys)
            {
                if (IsFirst)
                {
                    Graphics.FillRectangle(Brushes.Red, Item.X * Zoom, Item.Y * Zoom, Zoom, Zoom);

                    IsFirst = false;
                }
                else
                {
                    Graphics.FillRectangle(Brushes.White, Item.X * Zoom, Item.Y * Zoom, Zoom, Zoom);
                }
            }

            foreach (var Item in Snake.Foods)
            {
                Graphics.FillRectangle(Brushes.Yellow, Item.X * Zoom, Item.Y * Zoom, Zoom, Zoom);
            }

            G.DrawImage(Canvas, 0, 0);
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            Assist.Move(Snake);

            Snake.Next();

            using (var Graphics = this.CreateGraphics())
            {
                PrintFrame(Graphics);
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                    Snake.Direction = Directions.Left;
                    break;
                case Keys.Up:
                    Snake.Direction = Directions.Up;
                    break;
                case Keys.Right:
                    Snake.Direction = Directions.Right;
                    break;
                case Keys.Down:
                    Snake.Direction = Directions.Down;
                    break;
            }

            base.OnKeyDown(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            Graphics.Dispose();

            Canvas.Dispose();

            base.OnClosed(e);
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            BtnPlay.Left = (this.Width - BtnPlay.Width) / 2;
            BtnPlay.Top = (this.Height - BtnPlay.Height) * 2 / 3;

            if (this.IsHandleCreated)
            {
                using (var Graphics = this.CreateGraphics())
                {
                    PrintFrame(Graphics);
                }
            }

            base.OnSizeChanged(e);
        }

        private void BtnPlay_Click(object sender, EventArgs e)
        {
            BtnPlay.Visible = false;

            this.Focus();

            Snake.Play();

            Timer.Start();
        }
    }
}